<?php

class Goods extends Admin_Controller
{
    function __construct()
    {

        parent::__construct();
        error_reporting(E_ALL ^ E_NOTICE);
        $this->load->helper('url_helper');
        $this->load->helper('url');
        $this->load->model('goods_model','goods');
//        header("Access-Control-Allosw-Origin: http://runapi.showdoc.cc");
//        header("Access-Control-Allow-Credentials : true");
    }


    function goods_list(){
        $requst = $_REQUEST;
        $post_arr['key'] = md5_key();
        isset($requst['goods_cate']) ? $post_arr['goods_cate'] = $requst['goods_cate']:1;
        isset($requst['sort_type']) ? $post_arr['sort_type'] = $requst['sort_type']:1;
        isset($requst['search_text']) ? $post_arr['search_text'] = $requst['search_text']:1;
        isset($requst['sort_desc']) ? $post_arr['sort_desc'] = $requst['sort_desc']:1;
        isset($requst['page']) ? $post_arr['page'] = $requst['page']:1;
        isset($requst['psize']) ? $post_arr['psize'] = $requst['psize']:1;
        isset($requst['customer_id']) ? $post_arr['customer_id'] = $requst['customer_id']:1;
        $post_arr['type'] = isset($requst['type']) ? $requst['type'] : '';
//        var_dump($post_arr);exit;
        $url = $this->config->item("shop_base_url")."citymanagerapi/get_product_info/";
        $rs = curl_request($url,$post_arr);
        $rs = json_decode($rs,true);
        if ($rs['code']==200){
           $data['status'] = "success";
           $data['msg'] = "获取成功";
           $data['data'] = [];
            if(is_array($rs['data']))
            {
                foreach ($rs['data'] as $key=>$v){
                    $spec_arr = [];
                    if(isset($v['product_spec']) && !empty($v['product_spec']) && is_array($v['product_spec']))
                    {
                        foreach ($v['product_spec'] as $v1){
                            $spec_str=$v1['name'].":";
                            $spec_value=[];
                            foreach ($v1['attr'] as $key2=>$v2){
                                $spec_value[]=$v2['attr_value'];
                            }
                            $spec_str.=implode(",",$spec_value);
                            $spec_arr[]=$spec_str;
                        }
                    }
                    $rs['data'][$key]['spec_arr'] = $spec_arr;
                    unset( $rs['data'][$key]['product_spec']);
                    if(strstr($v['product_name'],'运费') ||strstr($v['product_name'],'邮费') ){
                    }else{
                        $data['data'][] =  $rs['data'][$key];
                    }
                }
            }
       }else{
           $data['status'] = "fail";
           $data['msg'] = $rs['msg'];
           $data['data'] = [];
       }
       json_send($data);

    }
    function goods_detail(){
        $requst = $this->input->get();
        $post_arr['key'] = md5_key();
//        $requst['goods_id']?$post_arr['id'] = $requst['goods_id']:"";
        $post_arr['id'] = $requst['id'] ? $requst['id'] : "";
        $post_arr['customer_id'] = $requst['customer_id'] ? $requst['customer_id'] : "";
        $url = $this->config->item("shop_base_url")."citymanagerapi/get_product_detail/";
        $rs = curl_request($url,$post_arr);
        $rs = json_decode($rs);
        if ($rs->code==200){
            $data['status'] = "success";
            $data['msg'] = "获取成功";
            $data['data'] = $rs->data;

        }else{
            $data['status'] = "fail";
            $data['msg'] = $rs->msg;
            $data['data'] = [];
        }
        json_send($data);

    }
    function goods_cate(){
        $requst = $this->input->post();
        $post_arr['key'] = md5_key();
        $url = $this->config->item("shop_base_url")."citymanagerapi/get_category/";
        $rs = curl_request($url,$post_arr);
        $rs = json_decode($rs);
        if ($rs->code==200){
            $data['status'] = "success";
            $data['msg'] = "获取成功";
            $data['data'] = $rs->data;

        }else{
            $data['status'] = "fail";
            $data['msg'] = $rs->msg;
            $data['data'] = [];
        }
        json_send($data);
    }

    //同步价格到商城
    function adjust_unit_price(){
        $post  = $this->input->post();
        if($post['parent_id'] && $post['ratio'] && $post['products']){
            $arr = array(
                "parent_id"=>$post['parent_id'],
                "ratio"=>$post['ratio'],
                "products"=>json_decode($post['products'],true)
            );
            $unit_price[] = $arr;
            $unit_price_json = json_encode($unit_price);
            $data = $this->goods->adjust_unit_price($post['customer_id'] , $unit_price_json);
        }else{
            $data['status'] = "fail";
            $data['msg'] = "参数错误";
            $data['data'] = "";
        }
        json_send($data);
    }

    //总价浮动
    function adjust_all_price(){
        $data= $this->goods->adjust_all_price($_GET['customer_id'],$_GET['ratio']);
        json_send($data);
    }

    function recove_price(){
        if($_GET['customer_id']){
            //同步到商城
            $url = $this->config->item("shop_base_url")."citymanagerapi/sync_recover_price/";
            $data1['key'] = md5_key();
            $data1['customer_id'] =$_GET['customer_id'];
            //发送数据
            $get_data = curl_request($url,$data1);
            $get_data = json_decode($get_data,true);
            if ($get_data['code']==200){
                $map['id'] = $_GET['customer_id'];
                $this->db->where($map)->update("customer",array("adjustments"=>0));  //此用户调价数
                $data['msg'] = "恢复成功";
                $data['status'] = "success";
                $data['data'] = "";
            }else{
                $data['msg'] = $get_data['msg'];
                $data['status'] = "fail";
                $data['data'] = "";
            }
        }else{
            $data['msg'] = "参数错误";
            $data['status'] = "fail";
            $data['data'] = "";
        }
        json_send($data);
    }

    function log_test(){
        log_message("error","验证码发送失败");
    }

    function adjusted_price_goods(){
        if($_GET['customer_id']){
            $info = $this->db->where(array("id"=>$_GET['customer_id']))->get("customer")->row_array();
            $return_data['adjustments'] = $info['adjustments']?$info['adjustments']:0;
            $_GET['page']?$post_arr['page'] = $_GET['page']:'';
            $_GET['psize']?$post_arr['psize'] = $_GET['psize']:'';
            $post_arr['key'] = md5_key();
            $post_arr['customer_id'] = $_GET['customer_id'];
            $url = $this->config->item("shop_base_url")."citymanagerapi/get_product_info/";
            $rs = curl_request($url,$post_arr);
            $rs = json_decode($rs,true);
            if($rs['code']=200){
                $data['status'] = "success";
                $data['msg'] = "获取成功";
                if($rs['data'] && count($rs['data'])){
                    foreach ($rs['data'] as $key=>$v){
                        $spec_arr = [];
                        if($v['product_spec']){
                            foreach ($v['product_spec'] as $v1){
                                $spec_str=$v1['name'].":";
                                $spec_value=[];
                                foreach ($v1['attr'] as $key2=>$v2){
                                    $spec_value[]=$v2['attr_value'];
                                }
                                $spec_str.=implode(",",$spec_value);
                                $spec_arr[]=$spec_str;
                            }
                            $rs['data'][$key]['spec_arr'] = $spec_arr;
                            unset( $rs['data'][$key]['product_spec']);

                        }else{
                            $rs['data'][$key]['spec_arr'] = [];
                        }


                    }
                }

                $return_data['list']=$rs['data']?$rs['data']:[];
                $data['data'] = $return_data;
            }else{
                $data['status'] = "fail";
                $data['msg'] = $rs['msg'];
                $data['data'] = "";
            }
        }else{
            $data['status'] = "fail";
            $data['msg'] = "参数错误";
            $data['data'] = "";
        }

        json_send($data);

    }
    //保存临时单品调价信息
    function save_temp_price(){
        $post = $this->input->post();
        if($post['save_key'] && $post['unit_price']){
            $unit_price = json_decode($post['unit_price'],true);
            if (!$unit_price['ratio']  || !count($unit_price['products']) || !$unit_price['parent_id']){
                $data['status'] = "fail";
                $data['msg'] = "参数错误";
                $data['data'] = "";
                json_send($data);exit;
            }
            if($unit_price['ratio']==100){
                $data['status'] = "fail";
                $data['msg'] = "调整比例不可为100";
                $data['data'] = "";
                json_send($data);exit;
            }
            $rs = $this->db->where(array("key"=>$post['save_key']))->get("key_value")->row_array();
            if($rs){
                $save_json = $rs['value'];
                //已经存储的调价信息
                $arrays = json_decode($save_json,true);
                //用户发送的调价信息
                foreach ($arrays as $key=>$v){
                    if($v['parent_id'] == $unit_price['parent_id']){
                        $arrays[$key] = $unit_price;
                        $exist_good = 1;
                    }
                }
                //如果调价商品没存在，新加一个
                if(!$exist_good ){
                    $arrays[] = $unit_price;
                }
                $upd = array("value"=>json_encode($arrays));
                $bool = $this->db->where(array("key"=>$post['save_key']))->update("key_value",$upd);
            }else{
                    $arrays[] = $unit_price;
                    $insert = array(
                        "key"=>$post['save_key'],
                        "value"=>json_encode($arrays)
                    );
                $bool = $this->db->insert("key_value",$insert);
            }
            if($bool){
                $data['status'] = "success";
                $data['msg'] = "保存成功";
                $data['data'] = "";
            }else{
                $data['status'] = "fail";
                $data['msg'] = "保存失败";
                $data['data'] = "";
            }
        }else{
            $data['status'] = "fail";
            $data['msg'] = "参数错误";
            $data['data'] = "";
        }
        json_send($data);
    }
    //取出临时单品调价信息
    function get_temp_price_count(){
        if($_GET['save_key']){
            $rs = $this->db->where(array("key"=>$_GET['save_key']))->get("key_value")->row_array();
            $data['status'] = "success";
            $data['msg'] = "获取成功";
            $count = count(json_decode($rs['value'],true));
            $data['data'] = $count;

        }else{
            $data['status'] = "fail";
            $data['msg'] = "参数错误";
            $data['data'] = "";
        }
        json_send($data);
    }

    //获取临时保存的调价信息
    function get_temp_price(){
        if($_GET['save_key']){
            $map['key'] = $_GET['save_key'];
            $rs = $this->db->where(array("key"=>$_GET['save_key']))->get("key_value")->row_array();
            $data['status'] = "success";
            $data['msg'] = "获取成功";
            if($rs){
                $data['data'] =  json_decode($rs['value'],true);
                if($goods_id = $_GET['goods_id']){
                    foreach ($data['data'] as $key=>$v){
                        if($v['parent_id']!=$goods_id){
                            unset($data['data'][$key]);
                        }
                    }
                }
                $data['data'] = array_values($data['data']);
            }else{
                $data['data'] = [];
            }


        }else{
            $data['status'] = "fail";
            $data['msg'] = "参数错误";
            $data['data'] = "";
        }
        json_send($data);
    }

}